package com.pengtu.gsj.service.impl.system;

import java.util.Collection;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.dao.system.UserDao;
import com.pengtu.gsj.entity.app.User;
import com.pengtu.gsj.exception.ControllerException;
import com.pengtu.gsj.security.SpringSecurityUtils;
import com.pengtu.gsj.service.system.UserService;

/**
 * @author zl
 * @version 创建时间：2017年2月5日 下午5:11:44 类说明
 */
@Transactional
@Service
public class UserServiceImpl implements UserService {

	private static Logger logger = LoggerFactory
			.getLogger(UserServiceImpl.class);

	@Autowired
	private UserDao userDao;

	@Transactional(readOnly = true)
	@Override
	public User getUserByUsername(String username) {
		return userDao.findUniqueBy("username", username);
	}

	@Transactional(readOnly = true)
	@Override
	public User getUserByName(String username) {
		return userDao.getUser(username);
	}

	@Override
	public void saveUser(User user) {
		userDao.clear();
		userDao.save(user);
	}

	@Override
	public User getUserById(String id) {
		return userDao.get(id);
	}

	@Override
	public void deleteUser(String id) {
		if (isSupervisor(id)) {
			logger.warn("操作员{}尝试删除超级管理员用户",
					SpringSecurityUtils.getCurrentUserName());
			throw new ControllerException("不能删除超级管理员用户");
		}
		userDao.delete(id); //这样会将记录直接从数据库中删除
//		User user = getUserById(id);
//		user.setEnable("0"); // 将该用户设置成不可用
//		saveUser(user);

	}

	/**
	 * 判断是否超级管理员.
	 */
	private boolean isSupervisor(String id) {
		return id.equals("1");
	}

	@Transactional(readOnly = true)
	@Override
	public User findUserByEmail(String email) {
		return userDao.findUniqueBy("email", email);
	}

	@Transactional(readOnly = true)
	@Override
	public String getNameByUsername(String username) {
		return userDao.findName(username);
	}

	@Override
	public List<User> getUsers() {
		return userDao.getAll();
	}

	@Transactional(readOnly = true)
	@Override
	public List<User> findUsers(QueryFilter filter) {
		return userDao.find(filter);
	}

	@Transactional(readOnly = true)
	@Override
	public Page<User> findUsers(Page<User> page, QueryFilter filter) {
		return userDao.findPage(page, filter);
	}

	@Override
	public boolean isLoginNameUnique(String newLoginName, String oldLoginName) {
		return userDao.isPropertyUnique("username", newLoginName, oldLoginName);
	}

	/**
	 * 检查email是否唯一.
	 * 
	 * @return newEmail在数据库中唯一或等于oldEmail时返回true.
	 */
	@Transactional(readOnly = true)
	@Override
	public boolean isregisterEmailUnique(String newEmail, String oldEmail) {
		return userDao.isPropertyUnique("email", newEmail, oldEmail);
	}

	@Transactional(readOnly = true)
	@Override
	public Collection<GrantedAuthority> loadUserAuthoritiesByName(
			String username) {
		return this.getUserByUsername(username).getAuthorities();
	}

	/**
	 * 
	 * getPasswordByUsername: 适用:根据用户名得到用户密码
	 * 
	 * @param username
	 * @return
	 * @exception
	 * @since 1.0.0
	 */
	@Transactional(readOnly = true)
	@Override
	public String getPasswordByUsername(String username) {
		return this.getUserByUsername(username).getPassword();
	}

	/**
	 * 根据角色查询用户
	 * getUserByRoleName:
	 * 适用:
	 * @param roleName
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Override
	@Transactional(readOnly = true)
	public List<User> getUserByRoleName(String roleName) {
		String hql = "select vo from User vo join vo.roleList roles where roles.roleName=?";
		Object[] objs = { roleName };
		return userDao.find(hql, objs);
	}

	@Override
	public void merge(User user) {
		userDao.merge(user);
	}

}
